<?php

namespace App\Console\Commands\OpenShop;

use App\Enums\OpenShop\InvestmentPromotion\MainProcessEnum;
use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPermissionAnchorModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSiteMissionModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationModel;
use App\Service\OpenShop\Feishu\ShopManualService;
use App\Service\OpenShop\OrderService;
use Illuminate\Console\Command;

class RefreshOrderSignPermission extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'open_shop:refresh_all_order_permission';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '重新刷新订单权限';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // 删除所有数据
        $orderIds = ['KD2022060441190249'];

        OpenShopSignOrderPermissionAnchorModel::query()
                                              ->whereIn('orderNo', $orderIds)
                                              ->delete();

        OpenShopSignOrderModel::query()
                              ->whereIn('orderNo', $orderIds)
                              ->with('baseInfoRel') // 选址
                              ->with('shopLocationRel') // 选址
                              ->with('spaceDesignRel') // 空间设计
                              ->with('checkAcceptRel') // 工程验收
                              ->with('siteMissionsRel') // 任务处理`
                              ->orderBy('createdAt', 'desc')
                              ->chunk(500, function ($orders) {
                                  foreach ($orders as $order) {
                                      $model                           = new OpenShopSignOrderPermissionAnchorModel();
                                      $model->orderNo                  = $order->orderNo;
                                      $model->signInvestmentManagerId  = $order->investmentManagerId ?: 0; // 招商经理
                                      $model->localInvestmentManagerId = (optional($order->shopLocationRel)->investmentManagerId) ?: 0; // 选址开发
                                      $model->provinceCode             = (optional($order->baseInfoRel)->chooseShopProvinceCode) ?: 0; // 省市区
                                      $model->cityCode                 = (optional($order->baseInfoRel)->chooseShopCityCode) ?: 0; // 招商经理
                                      $model->districtCode             = (optional($order->baseInfoRel)->chooseShopDistrictCode) ?: 0; // 招商经理
                                      $model->designUid                = (optional($order->spaceDesignRel)->designUid) ?: 0; // 招商经理
                                      $model->preparationUid           = (optional($order->spaceDesignRel)->preparationUid) ?: 0; // 招商经理
                                      $model->checkManagerId           = (optional($order->checkAcceptRel)->managerId) ?: 0; // 招商经理

                                      foreach ($order->siteMissionsRel as $mission) {
                                          switch ($mission->stepStatus) {
                                              case MissionTemplateEnum::STEP_STATUS_3:
                                                  $model->localInvestmentHandleUserId = $mission->dealUid;
                                                  break;
                                              case MissionTemplateEnum::STEP_STATUS_7:
                                                  $model->designUidHandleUserId = $mission->dealUid;
                                                  break;
                                              case MissionTemplateEnum::STEP_STATUS_11:
                                                  $model->checkManagerHandleUserId = $mission->dealUid;
                                                  break;
                                          }
                                      }

                                      $model->orderStatus = $order->status;
                                      $model->save();

//                                      $missionStatus = $order->status < 2 ? $order->status : 1;
//                                      // 刷新任务状态
//                                      OpenShopSiteMissionModel::query()->where('orderNo', $order->orderNo)
//                                                              ->update(['orderStatus' => $missionStatus]);
//
//
//                                      // 删除作废选址订单
//                                      if ($order->status == 0) {
//                                          OpenShopSiteLocationModel::query()->where('orderNo', $order->orderNo)
//                                                                   ->update(['isDeleted' => 1]);
//                                      }


                                      $this->info($model->orderNo);
                                  }
                              });

//        // 刷新任务的订单状态
//
//


        return 0;
    }
}
